<?php
/**
 * Created by PhpStorm.
 * User: cxj
 * Date: 16/12/28
 * Time: 下午1:05
 */
use app\assets\AppAsset;
use yii\helpers\Html;
use yii\helpers\Url;
$baseUrl = Yii::$app->request->baseUrl;
AppAsset::addCss($this, $baseUrl . "/css/publish.css");
AppAsset::addCss($this, $baseUrl . "/css/home.css");


?>

<style type="text/css">

</style>



<link href="../../umeditor/themes/default/css/umeditor.css" type="text/css" rel="stylesheet">
<script type="text/javascript" charset="utf-8" src="../../umeditor/umeditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="../../umeditor/umeditor.min.js"></script>
<script type="text/javascript" src="../../umeditor/lang/zh-cn/zh-cn.js"></script>



    <form name="form" id="form" hidden>
        <input type="file" name="img1" class="upload" id="upload" multiple="multiple">
    </form>
<div class="formdata">
    <form>
        <div class="inline" style="height: 190px">
            <label class="form-label" style="line-height: normal">新闻封面:</label>

<!--            <div class="poster" id="news_poster">-->
<!--            </div>-->
            <img src="" class="poster" id="news_poster" style="width: 280px;height: 190px;background-color: #283041;float: left">
                <div class="poster-select">
                    <input type="button" class="btn btn-default my-btn" id="fileBtn"
                           style="margin-left: 25px;margin-top: 90px" value="上传图片">
                    <p class="poster-tips">注：海报图片不能超过5M，并且不能小于750*558</p>
                </div>
        </div>




        <div class="inline">
            <label class="form-label">新闻标题:</label>
            <input class="input-control" type="text" placeholder="20字以内" id="newsTitle">
        </div>

        <div class="inline">
            <label class="form-label">活动详情:</label>
            <script type="text/plain" id="myEditor" style="width:600px;height:240px;"></script>
        </div>
        <div class="operation">
            <button class=" btn btn-default publish-btn" id="publish">发布</button>
            <button class=" btn btn-default" style="margin-left: 15px;">存为草稿</button>
            <button class=" btn btn-default " style="margin-left: 15px;">预览</button>
        </div>
    </form>
</div>






<script>
    $('#s').click(function () {
        $('.poster').css("background-image","url(../../img/gc-icon.jpeg)");
    });
    //初始化编辑器
    var um = UM.getEditor('myEditor');

    //获取输入内容
    function getContent() {
        return UM.getEditor('myEditor').getContent();
    }

    $('#publish').click(function () {
        //表单校验
        //发布存到数据库
        var newsPoster=$("#news_poster")[0].src;
        var newsTitle=$("#newsTitle").val();
        var newsDetail=getContent();
        console.log('data',newsDetail);
        console.log('newsPo',newsPoster);

        $.ajax({
            type:'POST',
            url: "news-publish",
            data:{
                "newsPoster":newsPoster,
                "newsTitle":newsTitle,
                "newsDetail":newsDetail
            },
            dataType: "json",
            success: function (data) {

                if(data=='0'){
                    alert('发布失败');
                }else{
                    alert('发布成功');
//                    location.href='index';
                }

            }
        });

    });

    $("#fileBtn").on('click',function(){
        $("#upload").click();
    });

    $("#upload").change(function(d){
        var formdata=new FormData($("#form")[0]);
        formdata.append("type","0");
        formdata.append("names", "img");
        var file = this.files[0];
        var file_name = file.name;
        var type = file_name.split(".")[1];
        if(type=="jpg"||type=="png"||type=="jpeg"){
            $.ajax({
                type : 'POST',
                url : './uploader',
                data: formdata,
                cache : false,
                processData : false,
                contentType : false,
                mimeType:"multipart/form-data",
                success : function(data){
                    if(data){
                        $('#news_poster').attr("src",""+data+"")
                    };

            },
                error:function(){
                    alert('上传失败');
                }


    })

    }
    })



</script>



